// ******************************************************************
// * Copyright(c)  Steven Lu
// * Description   : Entity_SYS_WORKING_PERIOD
// * CreateDate    : 2013-08-28 11:00:58
// * Creater       : CodeSmith
// * LastChangeDate: 2013-08-28 11:00:58
// * LastChanger   : 2013-08-28 11:00:58
// * Version Info  : 1.0 For ESOL
// ******************************************************************

using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Suzsoft.Smart.Data;
using ESOL.Common.Entity;

namespace ESOL.Business.Sys
{
/// <summary>
    /// SysWorkingPeriod
    /// </summary>
    public partial class SysWorkingPeriodBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objSysWorkingPeriodEntity">Entity</param>
        /// <returns>Entity</returns>
        public static SysWorkingPeriodEntity GetSysWorkingPeriod(SysWorkingPeriodEntity objSysWorkingPeriodEntity)
        {
            return DataAccess.SelectSingle<SysWorkingPeriodEntity>(objSysWorkingPeriodEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objSysWorkingPeriodEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static SysWorkingPeriodEntity InsertSysWorkingPeriod(SysWorkingPeriodEntity objSysWorkingPeriodEntity)
        {
            objSysWorkingPeriodEntity.Id = Guid.NewGuid().ToString();
            DataAccess.Insert(objSysWorkingPeriodEntity);
            return objSysWorkingPeriodEntity;
        }

        public static bool InsertSysWorkingPeriod(List<SysWorkingPeriodEntity> objSysWorkingPeriodEntityList)
        {
            return DataAccess.Insert(objSysWorkingPeriodEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objSysWorkingPeriodEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateSysWorkingPeriod(SysWorkingPeriodEntity objSysWorkingPeriodEntity)
        {
            return DataAccess.Update(objSysWorkingPeriodEntity);
        }

        public static bool UpdateSysWorkingPeriod(List<SysWorkingPeriodEntity> objSysWorkingPeriodEntityList)
        {
            return DataAccess.Update<SysWorkingPeriodEntity>(objSysWorkingPeriodEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objSysWorkingPeriodEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteSysWorkingPeriod(SysWorkingPeriodEntity objSysWorkingPeriodEntity)
        {
            return DataAccess.Delete(objSysWorkingPeriodEntity);
        }

        public static bool DeleteSysWorkingPeriod(List<SysWorkingPeriodEntity> objSysWorkingPeriodEntityList)
        {
            return DataAccess.Delete<SysWorkingPeriodEntity>(objSysWorkingPeriodEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetSysWorkingPeriodAll()
        {
            return DataAccess.SelectAllDataSet<SysWorkingPeriodEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<SysWorkingPeriodEntity> GetSysWorkingPeriodAllList()
        {
            return DataAccess.SelectAll<SysWorkingPeriodEntity>();
        }
        #endregion


        #region Functions
        public static DataSet GetSysWorkingPeriodAll(SysPeriodEntity sysPeriod)
        {
            string sql = "select * from sys_working_period where period_id='" + sysPeriod.Id + "'";
            return DataAccess.SelectDataSet(sql);
        }
        #endregion


        public static bool CreateCalendar(List<SysPeriodEntity> period, List<SysWorkingPeriodEntity> workingPeriod)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    if (period != null) DataAccess.Insert(period, broker);
                    if (workingPeriod != null) DataAccess.Insert(workingPeriod, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }

        public static bool UpdateCalendar(SysPeriodEntity period, List<SysWorkingPeriodEntity> workingPeriod)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    List<SysWorkingPeriodEntity> deleteWorkingPeriod = DataAccess.Select<SysWorkingPeriodEntity>(new SysWorkingPeriodEntity { PeriodId = period.Id });
                    broker.BeginTransaction();
                    if (deleteWorkingPeriod != null) DataAccess.Delete(deleteWorkingPeriod, broker);
                    if (period != null) DataAccess.Update(period, broker);
                    if (workingPeriod != null && workingPeriod.Count>0) DataAccess.Insert(workingPeriod, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }


        public static bool DeleteCalendar(SysPeriodEntity period)
        {
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    List<SysWorkingPeriodEntity> workingPeriod = DataAccess.Select<SysWorkingPeriodEntity>(new SysWorkingPeriodEntity { PeriodId = period.Id });
                    broker.BeginTransaction();
                    if (workingPeriod != null) DataAccess.Delete(workingPeriod, broker);
                    broker.Commit();
                    return true;
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    throw ex;
                }
            }
        }


        public static DataSet GetWorkingPeriod(int year, int month)
        {
            string sql = @" select WP.ID,WP.IS_ACTIVE, TIME_FROM,TIME_TO,PERIOD_ID,SHIFT_TYPE,CONVERT(DATETIME,convert(varchar,P.year)+'-'+convert(varchar,P.MONTH)+'-'+convert(varchar,P.DAY)) PERIOD_DATE ,
  FC.NAME 
   from dbo.SYS_WORKING_PERIOD WP LEFT JOIN  SYS_PERIOD P ON WP.PERIOD_ID=P.ID
   LEFT JOIN SYS_FC FC ON P.CALENDAR_ID=FC.ID
   where p.YEAR='" + year + "' and p.MONTH='" + month + "' order by PERIOD_DATE,TIME_FROM";
            return DataAccess.SelectDataSet(sql);
        }
    }
	
	
}	

